//
//  LHReadCollectDetailController.m
//  EPA
//
//  Created by 卢浩 on 16/4/26.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHReadCollectDetailController.h"
#import "LHReadCollectModel.h"
#import "LHDBManager.h"
#import "LHReadCommentController.h"

@interface LHReadCollectDetailController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation LHReadCollectDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.model.title;
    
    UIBarButtonItem *cancelCollectBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"取消收藏" style:(UIBarButtonItemStylePlain) target:self action:@selector(cancelCollectAction)];
    UIBarButtonItem *commentBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"评论" style:(UIBarButtonItemStylePlain) target:self action:@selector(commentAction)];
    self.navigationItem.rightBarButtonItems = @[commentBarBtn,cancelCollectBarBtn];

    self.webView.delegate = self;
    [self.webView loadHTMLString:self.model.html baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=380;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}

//评论按钮
- (void)commentAction{
    LHReadCommentController *readCommentVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LHReadCommentController"];
    readCommentVC.contentid = self.model.ID;
    [self.navigationController pushViewController:readCommentVC animated:YES];
}

//取消收藏按钮
- (void)cancelCollectAction{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要取消收藏吗?" preferredStyle:(UIAlertControllerStyleAlert)];
    [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [[LHDBManager shareManager] DeleteReadCollectWithContentID:self.model.ID];
        [self.navigationController popViewControllerAnimated:YES];
    }]];
    [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [self presentViewController:alertC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
